(function(){

  class Node{
      constructor(data){
          this.data = data;
          this.next =null
      }
  }  

  class LinkedList{
      constructor(){
        this.head = null;
        this.length = 0;
      }
       
      // 添加元素
      append(data){
         let newNode = new Node(data)
         this.length++;
         // 当链表为null时
         if(this.head == null){
            this.head = newNode
            return
         }
         // 当链表不为空时
         let current = this.head
         while(current.next != null){
              current = current.next; 
         }
         current.next = newNode;
      }
     // 转为字符串
      toString(){
          let str = "";
          let current = this.head
          while(current){
             str+=current.data;
             current = current.next
          } 
          return str
      }

      // 从某个位置插入
      insert(data,position){
        if( position<0 || position>this.length) return false

        let newNode = new Node(data);
        let current = this.head;               
        let index =  0;      
        let preCurrent = null;

        if(position === 0){
          this.head = newNode
          newNode.next =  current
          this.length+=1
          return true
        }
        
        while(index < position){
            preCurrent = current;
            current = current.next
            index++
        }
        preCurrent.next = newNode;
        newNode.next = current;
        this.length+=1
        return true
      } 
      // 根据位置返回数据
     getData(position){
        if( position<0 || position>=this.length || position == undefined) return false
         let current = this.head;
         let index = 0;        
         while(index < position){
             current = current.next
             index+=1   
         }
         return current.data
     }  
     // 根据内容获得索引
     indexOf(data){
        let current = this.head;
        let index = 0;    
        while( current.data != data){        
            if( current.next == null){           
               return false
            }   
            current = current.next;
            index+=1;
        }
        return index

     }

     // 更新指定位置的数据
     update(data,position){
        if( position<0 || position>=this.length || position == undefined) return false
        
         let current = this.head;
         let index = 0;
         while(index < position){
             current = current.next;
             index++
         }
         current.data = data
     }

     // 移除指定位置元素
     removeAt(position){
        if( position<0 || position>=this.length || position == undefined) return null
        
        let current = this.head;
        let index = 0;
        let preNode = null
        let nextNode = null 

        if(position === 0){
            nextNode = current.next;
            this.head = nextNode
            this.length-=1
            return current.data
        }

        while(index < position){
            preNode = current;
            current = current.next;
            nextNode = current.next
            index++
        }
        preNode.next = nextNode; 
        this.length-=1   
        return current.data
     }

     // 根据内容进行删除
     remove(data){
        let  current = this.head;
        let  index = 0;
        let  preNode = null;
        let  nextNode = null;

        while( current.data != data ){
            if(current.next == null){
                return false
            }
            preNode = current;
            current = current.next;
            nextNode = current.next;
            index+=1
        }
        if(index == 0){
            this.head = current.next
            this.length-=1
            return true
        }
        preNode.next = nextNode
        this.length-=1
        return true
     }

     isEmpty(){
         return this.length == 0? true: false
     }
     size(){
         return this.length
     }
     

  }


})()